home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / VideoToolbox 96.06.15 / VideoToolboxSources / ImageStatistics.c < prev    next >
Text File  |  1995-08-15  |  2KB  |  71 lines

  1. /*
  2. ImageStatistics.c
  3.  
  4. ImageStatistics returns min, max, mean, and meanSquare values of the pixels in
  5. the supplied rect, after clipping with the GWorld's portRect. The supplied
  6. rect is replaced by the clipped rect.
  7.  
  8. ImageEnergy returns the summed square value, over the clipped rect area, of
  9. the pixels after subtracting the supplied "background".
  10.  
  11. HISTORY:
  12. 10/27/94 wrote it
  13. 8/15/95 dgp changed "0.0/0.0" to NAN to work around bug in Symantec C++ reported by Bosco.
  14. */
  15. #include "VideoToolbox.h"
  16.  
  17. double ImageEnergy(GWorldPtr world,Rect *aRect,double background)
  18. {
  19.     double mean,meanSquare,area;
  20.  
  21.     ImageStatistics(world,aRect,NULL,NULL,&mean,&meanSquare);
  22.     area=(double)(aRect->right-aRect->left)*(aRect->bottom-aRect->top);
  23.     return area*(meanSquare-2*background*mean+background*background);
  24. }
  25.  
  26. void ImageStatistics(GWorldPtr world,Rect *rect
  27.     ,long *minPtr,long *maxPtr,double *meanPtr,double *meanSquarePtr)
  28. {
  29.     register long pix,pixMax,pixMin,meanL,meanSquareL;
  30.     register double mean,meanSquare;
  31.     double area;
  32.     int width;
  33.     register int i,j;
  34.     int error=0;
  35.     unsigned long pixels[1024];
  36.  
  37.     assert(StackSpace()>4000);
  38.     SectRect(rect,&world->portRect,rect);
  39.     if(EmptyRect(rect)){
  40.         mean=meanSquare=NAN;
  41.         pixMin=pixMax=0;
  42.     }else{
  43.         width=rect->right-rect->left;
  44.         assert(width<=sizeof(pixels)/sizeof(*pixels));
  45.         mean=meanSquare=0;
  46.         pixMin=LONG_MAX;
  47.         pixMax=LONG_MIN;
  48.         for(j=rect->top;j<rect->bottom;j++){
  49.             GetWindowPixelsQuickly((WindowPtr)world,rect->left,j,pixels,width);
  50.             meanL=0;
  51.             meanSquareL=0;
  52.             for(i=width-1;i>=0;i--){
  53.                 pix=pixels[i];
  54.                 if(pix<pixMin)pixMin=pix;
  55.                 if(pix>pixMax)pixMax=pix;
  56.                 meanL+=pix;
  57.                 meanSquareL+=pix*pix;
  58.             }
  59.             mean+=meanL;                // float
  60.             meanSquare+=meanSquareL;    // float
  61.         }
  62.         area=(double)width*(rect->bottom-rect->top);
  63.         mean/=area;
  64.         meanSquare/=area;
  65.     }
  66.     if(minPtr!=NULL)*minPtr=pixMin;
  67.     if(maxPtr!=NULL)*maxPtr=pixMax;
  68.     if(meanPtr!=NULL)*meanPtr=mean;
  69.     if(meanSquarePtr!=NULL)*meanSquarePtr=meanSquare;
  70. }
  71.